//
//  GCMReciveDetailViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/18.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMReciveDetailViewController.h"
#import "GCMAboutTaskViewController.h"
#import "PhotoViewController.h"

@interface GCMReciveDetailViewController ()<UIScrollViewDelegate>

@end

@implementation GCMReciveDetailViewController{

    UIScrollView *_scrollV;
    NSMutableArray *_dataArray;
    UIView *_View;
    UILabel *ZTLabel;
    UILabel *NameLabel;
    UIView *imageView;
    UIScrollView *_screollView1;
    UILabel *BSDXLabel;
    UILabel *BSRLabel;
    UILabel *LXDHLabel;
    UILabel *KHFYLabel;
    UILabel *YYSJLabel;
    UILabel *SLRLabel;
    UILabel *SLGSLabel;
    UIButton *_taskBtn;
    UIScrollView *_imageScrollV;
}
-(void)viewWillAppear:(BOOL)animated{
    if ([self.type isEqualToString:@"1"]) {
       self.navigationController.navigationBarHidden = NO;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self createScrollView];
    [self downLoadData];
}

-(void)downLoadData{

    
    [GCMReviceHttpRequst postDataWithreceiveId:self.idd Success:^(id obj, NSError *err) {
        //NSLog(@"%@",obj);
        _dataArray = obj;
        NSLog(@"相关任务的数据%@",_dataArray);
        [self createUI];
    }];
    
}

-(void)createImageScrollView{
    NSArray *array = [NSArray arrayWithArray:[[_dataArray valueForKey:@"images"] componentsSeparatedByString:@","]];
    //    创建可以滑动的视图
    _imageScrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 73, SCREEN_WIDTH- 40, 95)];
    _imageScrollV.delegate = self;
    _imageScrollV.alwaysBounceHorizontal = NO;
    _imageScrollV.alwaysBounceVertical = NO;
    _imageScrollV.showsHorizontalScrollIndicator = NO;
    _imageScrollV.showsVerticalScrollIndicator = NO;
    _imageScrollV.bounces = NO;
    _imageScrollV.contentOffset = CGPointMake(0, 0);
    _imageScrollV.contentSize = CGSizeMake((array.count - 1) * 82, 95);
    self.automaticallyAdjustsScrollViewInsets = NO;
    [_View addSubview:_imageScrollV];
    for (int i=1; i<array.count; i++) {
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.frame = CGRectMake(82*(i- 1), 0, 80, 95);
        
        [imageV sd_setImageWithURL:[NSURL URLWithString:array[i]]placeholderImage:nil];
//        CLog(@"//////%@",_imageArray[i]);
        UITapGestureRecognizer* singleRecognizer;
        imageV.userInteractionEnabled = YES;
        singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgclick:)];
        //点击的次数
        singleRecognizer.numberOfTapsRequired = 1;
        imageV.tag = i +1;
        [imageV addGestureRecognizer:singleRecognizer];
        [_imageScrollV addSubview:imageV];
    }
}

-(void)imgclick:(UITapGestureRecognizer*)sender{
    
    //创建第三个界面
    PhotoViewController * photo = [[PhotoViewController alloc]init];
    photo.imageStr = [_dataArray valueForKey:@"images"];
    //获取当前点击的图片
    UIImageView * iv = (UIImageView*)sender.view;
    photo.index = iv.tag - 1;
    [self.navigationController pushViewController:photo animated:YES];

}

    


-(void)createScrollView{
    
    //    创建可以滑动的视图
    _scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 )];
    _scrollV.delegate = self;
    _scrollV.pagingEnabled = YES;
    _scrollV.alwaysBounceHorizontal = NO;
    _scrollV.alwaysBounceVertical = NO;
    _scrollV.showsHorizontalScrollIndicator = NO;
    _scrollV.showsVerticalScrollIndicator = NO;
    _scrollV.bounces = YES;
    _scrollV.contentOffset = CGPointMake(0, 0);
    _scrollV.contentSize = CGSizeMake(SCREEN_WIDTH, 600 );
    _scrollV.backgroundColor = [UIColor colorWithRed:.95 green:.95 blue:.95 alpha:1.0];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:_scrollV];
    
}

-(void)initData{

    _dataArray = [[NSMutableArray alloc]init];
    
    
}

-(void)createUI{
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 30)];
    titleLabel.text = [NSString stringWithFormat:@"%@%@%@",[_dataArray valueForKey:@"source"],[_dataArray valueForKey:@"type"],[_dataArray valueForKey:@"time"]];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [_scrollV addSubview:titleLabel];

    _View = [[UIView alloc]initWithFrame:CGRectMake(10, 40, SCREEN_WIDTH  - 20, SCREEN_HEIGHT)];
    _View.backgroundColor = [UIColor whiteColor];
    _View.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _View.layer.borderWidth  = 1;
    _View.layer.cornerRadius = 10;
    [_scrollV addSubview:_View];
    
    
    
    ZTLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, _View.bounds.size.width, 30)];
    ZTLabel.text = [NSString stringWithFormat:@"接待状态 : %@",[_dataArray valueForKey:@"state"]];
    ZTLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:ZTLabel];
    UILabel *label1  = [[UILabel alloc]initWithFrame:CGRectMake(5, 40, SCREEN_WIDTH - 30, 1)];
    label1.layer.borderWidth = 1;
    label1.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label1];
    
    NameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, _View.bounds.size.width - 20, 30)];
    NameLabel.text = [NSString stringWithFormat:@"  %@",[_dataArray valueForKey:@"content"]];
    NameLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:NameLabel];
    UILabel *label2  = [[UILabel alloc]initWithFrame:CGRectMake(5, 70, SCREEN_WIDTH - 30, 1)];
    label2.layer.borderWidth = 1;
    label2.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label2];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH - 30, 100)];
    [_View addSubview:imageView];
    [self createImageScrollView];
    
    UILabel *label3  = [[UILabel alloc]initWithFrame:CGRectMake(5, imageView.frame.origin.y + imageView.bounds.size.height, SCREEN_WIDTH - 30, 1)];
    label3.layer.borderWidth = 1;
    label3.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label3];
    
    BSDXLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, label3.frame.origin.y, _View.bounds.size.width, 30)];
    BSDXLabel.text = [NSString stringWithFormat:@"报事对象 : %@",[_dataArray valueForKey:@"address"]];
    BSDXLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:BSDXLabel];
    UILabel *label4  = [[UILabel alloc]initWithFrame:CGRectMake(5, label3.frame.origin.y +30, SCREEN_WIDTH - 30, 1)];
    label4.layer.borderWidth = 1;
    label4.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label4];
    
    BSRLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, label3.frame.origin.y + 30 , _View.bounds.size.width, 30)];
    BSRLabel.text = [NSString stringWithFormat:@"报事人    : %@",[_dataArray valueForKey:@"contact"]];
    BSRLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:BSRLabel];
    UILabel *label5  = [[UILabel alloc]initWithFrame:CGRectMake(5, label3.frame.origin.y +60, SCREEN_WIDTH - 30, 1)];
    label5.layer.borderWidth = 1;
    label5.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label5];
    
    LXDHLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, label3.frame.origin.y + 60 , _View.bounds.size.width, 30)];
    LXDHLabel.text = [NSString stringWithFormat:@"联系电话 : %@",[_dataArray valueForKey:@"tel"]];
    LXDHLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:LXDHLabel];
    UILabel *label6  = [[UILabel alloc]initWithFrame:CGRectMake(5, label3.frame.origin.y +90, SCREEN_WIDTH - 30, 1)];
    label6.layer.borderWidth = 1;
    label6.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label6];
    
    KHFYLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, label3.frame.origin.y + 90 , _View.bounds.size.width, 30)];
    KHFYLabel.text = [NSString stringWithFormat:@"客户反应 : %@",[_dataArray valueForKey:@"reaction"]];
    KHFYLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:KHFYLabel];
    UILabel *label7  = [[UILabel alloc]initWithFrame:CGRectMake(5, label3.frame.origin.y +120, SCREEN_WIDTH - 30, 1)];
    label7.layer.borderWidth = 1;
    label7.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label7];
    
    YYSJLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, label3.frame.origin.y + 120 , _View.bounds.size.width, 30)];
    YYSJLabel.text = [NSString stringWithFormat:@"预约时间 : %@",[_dataArray valueForKey:@"orderTime"]];
    YYSJLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:YYSJLabel];
    UILabel *label8  = [[UILabel alloc]initWithFrame:CGRectMake(5, label3.frame.origin.y +150, SCREEN_WIDTH - 30, 1)];
    label8.layer.borderWidth = 1;
    label8.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label8];
    
    SLRLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, label3.frame.origin.y + 150 , _View.bounds.size.width, 30)];
    SLRLabel.text = [NSString stringWithFormat:@"受理人    : %@",[_dataArray valueForKey:@"receiver"]];
    SLRLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:SLRLabel];
    UILabel *label9  = [[UILabel alloc]initWithFrame:CGRectMake(5, label3.frame.origin.y +180, SCREEN_WIDTH - 30, 1)];
    label9.layer.borderWidth = 1;
    label9.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label9];
    
    SLGSLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, label3.frame.origin.y + 180 , _View.bounds.size.width, 30)];
    SLGSLabel.text = [NSString stringWithFormat:@"受理公司 : %@",[_dataArray valueForKey:@"accepterCom"]];
    SLGSLabel.textColor= [UIColor lightGrayColor];
    [_View addSubview:SLGSLabel];
    UILabel *label10  = [[UILabel alloc]initWithFrame:CGRectMake(5, label3.frame.origin.y +210, SCREEN_WIDTH - 30, 1)];
    label10.layer.borderWidth = 1;
    label10.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_View addSubview:label10];
    
    
    _View.frame  = CGRectMake(10, 40, SCREEN_WIDTH  - 20, label10.frame.origin.y +10);
    
    _taskBtn  = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 100, _View.frame.origin.y + 20 + _View.bounds.size.height, 200, 40)];
    _taskBtn.backgroundColor = [UIColor colorWithRed:0 green:0.67 blue:0.65 alpha:1];
    [_taskBtn setTitle:@"相关任务" forState:UIControlStateNormal];
    [_taskBtn addTarget:self action:@selector(onTaskBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_scrollV addSubview:_taskBtn];


    
}

-(void)onTaskBtnClick{

    GCMAboutTaskViewController *about =[[GCMAboutTaskViewController alloc]init];
    about.idd = [_dataArray valueForKey:@"id"];
    about.taskAboutArray = _dataArray;
    [self.navigationController pushViewController:about animated:YES];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"相关任务";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;

}

//-(void)createScrollV{
//    
//    UIView *v = (UIView *)[self.view viewWithTag:1995];
//    UIScrollView *scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 24, v.bounds.size.height)];
//    scrollV.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.93 alpha:1];
//    // scrollV.backgroundColor = [UIColor lightGrayColor];
//    scrollV.delegate = self;
//    
//    scrollV.pagingEnabled = YES;
//    scrollV.bounces = YES;
//    scrollV.contentOffset = CGPointMake(0, 0);
//    scrollV.contentSize = CGSizeMake(self.imageArray.count *85, v.bounds.size.height);
//    
//    [v addSubview:scrollV];
//    
//    for (int i=0; i<_imageArray.count; i++) {
//        UIImageView *imageV = [[UIImageView alloc]init];
//        imageV.frame = CGRectMake(85*i, 0, 80, v.bounds.size.height);
//        
//        [imageV sd_setImageWithURL:[NSURL URLWithString:_imageArray[i]]placeholderImage:nil];
//        CLog(@"//////%@",_imageArray[i]);
//        UITapGestureRecognizer* singleRecognizer;
//        imageV.userInteractionEnabled = YES;
//        singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgclick:)];
//        //点击的次数
//        singleRecognizer.numberOfTapsRequired = 1;
//        imageV.tag = i +666;
//        [imageV addGestureRecognizer:singleRecognizer];
//        [scrollV addSubview:imageV];
//    }
//}
//
//-(void)imgclick:(UITapGestureRecognizer*)sender{
//    
//    XWJWebViewController * web = [[XWJWebViewController alloc] init];
//    UITapGestureRecognizer * singleTap = (UITapGestureRecognizer *)sender;
//    NSString *urls = _imageArray[(long)[singleTap view].tag-666]==[NSNull null]?@"":_imageArray[(long)[singleTap view].tag-666];
//    
//    NSArray *url = [urls componentsSeparatedByString:@","];
//    web.url = [url objectAtIndex:0];
//    //   self.shareImageStr = [url firstObject];
//    [self.navigationController pushViewController:web animated:NO];
//}
//

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
